package com.iot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iot.core.JsonHelper;
import com.iot.core.LResult;
import com.iot.core.PageData;
import com.iot.pojo.MTheartbeat;
import com.iot.service.TheartbeatService;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/system/heartbeat/")
public class THeartbeatController extends BaseController {
    @Autowired
    private TheartbeatService service;
    @ResponseBody
    @RequestMapping("save")
    public String save(@RequestParam String bean) {
        try {

        MTheartbeat entity = JsonHelper.toBean(bean, MTheartbeat.class);
        if (entity.getHEARTBEAT_ID() > 0) {
            service.update(entity);
        } else {
            service.insert(entity);

        }
            return LResult.Success(entity.getHEARTBEAT_ID()).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }
    @ResponseBody
    @RequestMapping("get")
    public String getByid(@RequestParam Integer id) {

        try {
            return LResult.Success(service.single(id)).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }
    @ResponseBody
    @RequestMapping("query")
    public String queryByPage() {

        try {
            PageData map=getPageData();
            PageHelper.startPage(Common.toInt(map.get("page")), Common.toInt(map.get("limit")));
            List<MTheartbeat> list = service.query();
            PageInfo<MTheartbeat> pageInfo = new PageInfo<>(list);
            return LResult.Success(pageInfo.getTotal(), pageInfo.getList()).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    }
